package br.brpv.silvestrin.vendas.mainactivity;

import android.app.Activity;
import android.view.View;
import android.widget.*;
import br.brpv.silvestrin.vendas.R;
import br.brpv.silvestrin.vendas.domain.*;
import br.brpv.silvestrin.vendas.manager.ClientManager;
import br.brpv.silvestrin.vendas.webservice.ServerDataCacheService;

import java.util.ArrayList;


public class SelectClientScreen implements AdapterView.OnItemClickListener, ClientManager.ClientManagerInterface {

    private SearchView searchView;
    private ListView clientsListView;
    private ListView establishmentList;
    private EditText codClientET;
    private EditText codEstablishmentET;
    private EditText razaoSocialET;
    private EditText nomeFantasiaET;
    private EditText enderecoET;
    private EditText bairroET;
    private EditText cidadeET;
    private EditText cepET;
    private EditText cnpjCpfET;
    private EditText inscricaoEstadualET;
    private Button nextButton;

    private Activity mActivity;

    private ClientAdapter clientAdapter = null;
    private EstablishmentAdapter estabAdapter = null;
    private Client selectedClient;
    private Establishment selectedEstablishment;
    private int codSelectedCompany;

    private ClientManager clientManager;

    private LinearLayout selectClientLayout;
    private ServerDataCacheService mServerDataCacheBinder;

    private SelectClientInterface selectClientInterface;

    public SelectClientScreen(Activity activity, SelectClientInterface selectClientInterface) {
        mActivity = activity;

        clientManager = new ClientManager(activity, this, null);

        estabAdapter = new EstablishmentAdapter(mActivity, new ArrayList<Establishment>());
        this.selectClientInterface = selectClientInterface;
    }

    public void initSelectClientScreen(){
        if(selectClientLayout != null){
            clientsListView = (ListView) selectClientLayout.findViewById(R.id.client_list);
            if(clientsListView != null) {
                clientsListView.setAdapter(clientAdapter);
                clientsListView.setOnItemClickListener(this);
            }

            establishmentList = (ListView)selectClientLayout.findViewById(R.id.establishment_list);
            if(establishmentList != null){
                establishmentList.setAdapter(estabAdapter);
                establishmentList.setOnItemClickListener(this);
            }

            searchView = (SearchView) selectClientLayout.findViewById(R.id.client_search_view);
            QueryItemListener queryItemListener = new QueryItemListener();
            searchView.setOnQueryTextListener(queryItemListener);
            searchView.setOnCloseListener(queryItemListener);

            codClientET = (EditText) selectClientLayout.findViewById(R.id.client_code);
            codEstablishmentET = (EditText) selectClientLayout.findViewById(R.id.client_code_estab);
            razaoSocialET = (EditText) selectClientLayout.findViewById(R.id.client_estab_name);
            nomeFantasiaET = (EditText) selectClientLayout.findViewById(R.id.client_fantasy_name);
            enderecoET = (EditText) selectClientLayout.findViewById(R.id.client_estab_address);
            bairroET = (EditText) selectClientLayout.findViewById(R.id.client_estab_address_bairro);
            cidadeET = (EditText) selectClientLayout.findViewById(R.id.client_estab_city_address);
            cepET = (EditText) selectClientLayout.findViewById(R.id.client_estab_zip_address);
            cnpjCpfET = (EditText) selectClientLayout.findViewById(R.id.client_estab_cnpj);
            inscricaoEstadualET = (EditText) selectClientLayout.findViewById(R.id.client_estab_ie);
            nextButton = (Button) selectClientLayout.findViewById(R.id.client_select_button);
            if(nextButton != null){
                if(selectedEstablishment == null) nextButton.setEnabled(false);
                nextButton.setOnClickListener(new NextButtonListener());
            }

        }
    }

    public void initClientsList(ArrayList<Client> clients) {

        clientAdapter = new ClientAdapter(mActivity, clients);
        clientAdapter.notifyDataSetChanged();

        if(clientsListView != null) {
            clientsListView.setAdapter(clientAdapter);
        }
    }

    public void getClientList(){
        clientManager.getClientList();
    }

    public void getEstabParamsForEditOrder(String clientId, String estabId) {
        clientManager.getClientDefaultParams(clientId, estabId);
    }

    private void updateEstablishmentDetails(Establishment clicked) {
        selectedEstablishment = clicked;
        if(nextButton!=null) nextButton.setEnabled(true);

        if(selectClientLayout != null) {
            codClientET.setText(clicked.getCodClient());
            codEstablishmentET.setText(clicked.getCodEstablishment());
            razaoSocialET.setText(clicked.getRazaoSocial());
            nomeFantasiaET.setText(clicked.getNomeFantasia());
            enderecoET.setText(clicked.getEndereco());
            bairroET.setText(clicked.getBairro());
            cidadeET.setText(clicked.getCidade());
            cepET.setText(clicked.getCep());
            cnpjCpfET.setText(clicked.getCnpjCpf());
            inscricaoEstadualET.setText(clicked.getInscricaoEstadual());
        }
    }


    private void cleanEstablishmentDetails(){
        if(selectClientLayout != null){
            if(codClientET != null) codClientET.setText("");
            if(codEstablishmentET != null) codEstablishmentET.setText("");
            if(razaoSocialET != null) razaoSocialET.setText("");
            if(nomeFantasiaET != null) nomeFantasiaET.setText("");
            if(enderecoET != null) enderecoET.setText("");
            if(bairroET != null) bairroET.setText("");
            if(cidadeET != null) cidadeET.setText("");
            if(cepET != null) cepET.setText("");
            if(cnpjCpfET != null) cnpjCpfET.setText("");
            if(inscricaoEstadualET != null) inscricaoEstadualET.setText("");
        }
    }


    /******Listeners*******************/
    @Override
    public void onClientsListReturn(ArrayList<Client> clientsList) {
        initClientsList(clientsList);

    }

    @Override
    public void onEstablishmentsListReturn(ArrayList<Establishment> estabs) {
        estabAdapter.clear();
        estabAdapter.addAll(estabs);
    }

    @Override
    public void onDefaultEstablishmentReturn(Establishment establishment) {
        updateEstablishmentDetails(establishment);

    }

    @Override
    public void onEstablishmentParamsReturn(EstabParamsDefault estabParamsDefault) {
        selectClientInterface.onEstabParamsReturn(estabParamsDefault);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Object selected = parent.getItemAtPosition(position);

        if(selected.getClass() == Client.class) {
            selectedClient = (Client) selected;
            estabAdapter.clear();
            cleanEstablishmentDetails();

            clientManager.getEstablishmentsList(selectedClient);
            clientManager.getEstablishmentDefault(selectedClient);

        } else if (selected.getClass() == Establishment.class) {
            selectedEstablishment = (Establishment) selected;
            updateEstablishmentDetails(selectedEstablishment);
        }
    }

    private class QueryItemListener implements SearchView.OnQueryTextListener, SearchView.OnCloseListener {
        @Override
        public boolean onQueryTextChange(String newText) {
            showResults(newText);
            return false;
        }

        @Override
        public boolean onQueryTextSubmit(String query) {
            showResults(query);
            return false;
        }

        private void showResults(String query) {
            if(clientAdapter != null) {
                clientAdapter.getFilter().filter(query);
            };
        }

        @Override
        public boolean onClose() {
            showResults("");
            return false;
        }
    }

    private class NextButtonListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            selectClientInterface.onClientSelected(selectedClient, selectedEstablishment);
            clientManager.getClientDefaultParams(selectedEstablishment.getClientId(), selectedEstablishment.getEstablishmentId());
            clientManager.checkPaymentLate(selectedEstablishment);
        }
    }
    /**********************************/


    /*******Getters and Setters********/

    public void setSelectClientLayout(LinearLayout selectClientLayout) {
        this.selectClientLayout = selectClientLayout;
    }

    public void setCodSelectedCompany(int codSelectedCompany) {
        if(this.codSelectedCompany != codSelectedCompany) {
            this.codSelectedCompany = codSelectedCompany;

            selectedClient = null;
            estabAdapter.clear();
            cleanEstablishmentDetails();
            if(searchView != null) searchView.setQuery("", false);
            if(nextButton != null) nextButton.setEnabled(false);

            clientManager.setCodSelectedCompany(codSelectedCompany);

            getClientList();
        }
    }

    public void setmServerDataCacheBinder(ServerDataCacheService mServerDataCacheBinder) {
        this.mServerDataCacheBinder = mServerDataCacheBinder;
        clientManager.setmServerDataCacheBinder(mServerDataCacheBinder);
    }

    public void setVisible(){
        if(selectClientLayout!=null) selectClientLayout.setVisibility(View.VISIBLE);
    }
    public void setInvisible(){
        if(selectClientLayout!=null) selectClientLayout.setVisibility(View.INVISIBLE);
    }

    /**********************************/

}
